<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">参数</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="参数"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">编号</label>
        <el-input
          v-model="query.number"
          clearable
          placeholder="编号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">批号</label>
        <el-input
          v-model="query.batchNumber"
          clearable
          placeholder="批号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">成分</label>
        <el-input
          v-model="query.ingredientData"
          clearable
          placeholder="成分"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">类型</label>
        <el-select
          v-model="query.state"
          clearable
          style="width: 185px;"
          placeholder="类型"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option value="1" label="入库" />
          <el-option value="2" label="出库" />
          <el-option value="3" label="归还" />
          <el-option value="4" label="销库" />
          <el-option value="5" label="盘库" />
        </el-select>
        <label class="el-form-item-label">操作人</label>
        <el-select
          v-model="query.userId"
          clearable
          style="width: 185px"
          placeholder="操作人"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in users"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
        <label class="el-form-item-label">时间</label>
        <date-range-picker v-model="query.createdAt" class="date-item" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        />
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <!-- <el-table-column type="selection" width="55" /> align="center" -->
        <el-table-column prop="name" label="参数"  align="center"/>
        <el-table-column label="条码" prop="barCode" align="center">
          <template slot-scope="scope">
           <div class="barCode">
            <el-image
              :src="
                baseUrl +
                '/api/barCode/showBarCode?id=' +
                scope.row.hcId +
                '&type=' +
                scope.row.type
              "
              style="height: 40px; vertical-align: middle"
              :preview-src-list="srcList"
              @click="enlargeBarcode(scope.row)"
            />
            <span>{{ scope.row.id }}</span>
           </div>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.state == 1">入库</span>
            <span v-if="scope.row.state == 2">出库</span>
            <span v-if="scope.row.state == 3">归还</span>
            <span v-if="scope.row.state == 4">销库</span>
            <span v-if="scope.row.state == 5">盘库</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="state" label="状态" /> -->
        <el-table-column prop="amount" label="数量" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.amount }}</span>
            <span v-if="scope.row.unit == 1">(支)</span>
            <span v-if="scope.row.unit == 2">(瓶)</span>
          </template>
        </el-table-column>
        <el-table-column prop="detailNumber" label="实际量" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.detailNumber }}</span>
            <span v-if="scope.row.unit == 1">(支)</span>
            <span v-if="scope.row.unit == 2">(mL)</span>
          </template>
        </el-table-column>
        <el-table-column prop="number" label="编号"  align="center"/>
        <el-table-column prop="batchNumber" label="批号"  align="center"/>
        <el-table-column prop="matrix" label="基体"  align="center"/>
        <el-table-column prop="expirationDate" label="有效期"  align="center" width="135"/>
        <el-table-column prop="cabinetName" label="冰箱" align="center" />
        <el-table-column prop="ingredientData" label="成分" width="100px" align="center">
          <template slot-scope="scope">
           <el-button type="primary" plain @click="showIngredientDialog(scope.row.ingredientData)">查看成分</el-button>
          </template>
        </el-table-column>
        <el-table-column  label="操作人"  align="center" prop="userName"></el-table-column>
        <el-table-column prop="createdAt" label="时间"  align="center" width="135"/>
        <!-- <el-table-column v-if="checkPer(['admin','hazardousChemicalsChangeRecord:edit','hazardousChemicalsChangeRecord:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column> -->
      </el-table>
      <!--分页组件-->
      <pagination ref="pag" />
    </div>
    <ingredientInfoDialog :info="currentShowIngredient" :ingredientDialog="ingredientDialog" @close="ingredientDialog=false"/>
  </div>
</template>

<script>
import crudHazardousChemicalsChangeRecord from "@/api/hazardousChemicalsChangeRecord";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import DateRangePicker from "@/components/DateRangePicker";
import ingredientInfoDialog from "@/components/ingredientInfoDialog.vue";
import { allNamelist } from "@/api/user";

const defaultForm = {
  id: null,
  name: null,
  hcId: null,
  type: null,
  state: null,
  amount: null,
  createdAt: null,
  updatedAt: null
};
export default {
  name: "StandardSubstanceChangeRecord",
  components: {
    pagination,
    crudOperation,
    rrOperation,
    udOperation,
    DateRangePicker,
    ingredientInfoDialog
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: "标准物质变更记录",
      url: "api/hazardousChemicalsChangeRecord",
      idField: "id",
      sort: "id,desc",
      crudMethod: { ...crudHazardousChemicalsChangeRecord },
      query: {
        type: 2
      },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: true,
        reset: true
      }
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "hazardousChemicalsChangeRecord:add"],
        edit: ["admin", "hazardousChemicalsChangeRecord:edit"],
        del: ["admin", "hazardousChemicalsChangeRecord:del"]
      },
      rules: {},
      queryTypeOptions: [
        { key: "name", display_name: "化学品名称" },
        { key: "type", display_name: "类型" },
        { key: "state", display_name: "状态" }
      ],
      ingredientDialog: false,
      currentShowIngredient:[],
      users: [],
      srcList: [], //预览图片列表
      baseUrl: process.env.VUE_APP_BASE_API,
    };
  },
  created() {
    this.getUsers();
  },
  methods: {
    showIngredientDialog(info){
      this.ingredientDialog = true
      this.currentShowIngredient = info
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
    [CRUD.HOOK.afterRefresh]() {
      this.crud.data = this.crud.data.map(
        item =>
          (item = {
            ...item,
            unit: Number(item.unit),
            ingredientData: (item.ingredientData&&JSON.parse(item.ingredientData))|| []
          })
      );
      return true;
    },
    getUsers() {
      allNamelist()
        .then((response) => {
          this.users = response.content;
        })
        .catch((_) => {
          console.log(response);
        });
    },
    enlargeBarcode(row) {
      var arr =
        this.baseUrl +
        "/api/barCode/showBarCode?id=" +
        row.id +
        "&type=" +
        row.type;
      this.srcList = arr.split(";");
    },
  }
};
</script>

<style scoped lang="scss">
</style>
